package com.tuyrt.`as`.proj.main.fragment.category

import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Rect
import android.text.TextUtils
import android.view.View
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.State
import com.tuyrt.hi.library.util.HiDisplayUtil

/**
 * author: Administrator on 2021/7/8.
 * desc:
 */
class CategoryItemDecoration(val spanCount: Int,val callback: (Int) -> String) : RecyclerView.ItemDecoration() {

    // 存储每一组第一个item所在组的名称 groupName 和 位置 position
    private val groupFirstPosition = mutableMapOf<String, Int>()

    private val paint = Paint(Paint.ANTI_ALIAS_FLAG)

    init {
        paint.style = Paint.Style.FILL
        paint.color = Color.BLACK
        paint.textSize = HiDisplayUtil.sp2px(15f).toFloat()
    }

    override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: State) {
        // 根据view 对象，找到它在列表中的位置 adapterPosition
        val adapterPosition = parent.getChildAdapterPosition(view)

        if (adapterPosition >= parent.adapter!!.itemCount || adapterPosition < 0) return

        // 2. 拿到 当前位置adapterPosition 对应的组名 groupName
        val groupName = callback(adapterPosition)
        // 3. 拿到 前一个item 的组名
        val preGroupName = if (adapterPosition > 0) callback(adapterPosition - 1) else null

        // 判断是否在同一组
        val sameGroup = TextUtils.equals(groupName, preGroupName)
        if (!sameGroup && !groupFirstPosition.containsKey(groupName)) {
            // 说明当前位置 adapterPosition 对应的item 是 当前组的 第一个位置
            // 此时 应该存储起来，记录下来，目的是为了方便后面计算，计算 后面item 是否是第一行
            groupFirstPosition[groupName] = adapterPosition
        }

        val firstRowPosition = groupFirstPosition[groupName] ?: 0
        // 判断与每组的第一个item 是否在同一行
        val sameRow = adapterPosition - firstRowPosition in 0 until spanCount // 3 -> [0,2]

        if (!sameGroup || sameRow) {
            outRect.set(0, HiDisplayUtil.dp2px(40f), 0, 0)
            return
        }
        outRect.set(0, 0, 0, 0)
    }

    override fun onDrawOver(c: Canvas, parent: RecyclerView, state: State) {
        val childCount = parent.childCount
        for (index in 0 until childCount) {
            val view = parent.getChildAt(index)
            val adapterPosition = parent.getChildAdapterPosition(view)
            if (adapterPosition >= parent.adapter!!.itemCount || adapterPosition < 0) continue
            val groupName = callback(adapterPosition)

            // 判断当前位置 是不是分组的第一个位置
            // 如果是，咱们在它的位置上绘制标题
            val groupFirstPosition = groupFirstPosition[groupName]
            if (groupFirstPosition == adapterPosition) {
                val decorationBounds = Rect()
                //为了拿到当前item 的 左上右下的坐标信息 包含了，margin 和 扩展空间的
                parent.getDecoratedBoundsWithMargins(view, decorationBounds)

                val textBounds = Rect()
                // 拿到文本的高度
                paint.getTextBounds(groupName, 0, groupName.length, textBounds)

                c.drawText(
                    groupName,
                    HiDisplayUtil.dp2px(16f).toFloat(),
                    (decorationBounds.top + 2 * textBounds.height()).toFloat(),
                    paint
                )
            }
        }
    }

    fun clear() {
        groupFirstPosition.clear()
    }
}